package com.hanbang.maige.view.quanzi.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.search.core.PoiInfo;
import com.baidu.mapapi.search.geocode.GeoCodeResult;
import com.baidu.mapapi.search.geocode.GeoCoder;
import com.baidu.mapapi.search.geocode.OnGetGeoCoderResultListener;
import com.baidu.mapapi.search.geocode.ReverseGeoCodeOption;
import com.baidu.mapapi.search.geocode.ReverseGeoCodeResult;
import com.hanbang.maige.R;
import com.hanbang.maige.code.base.baseadapter.ViewHolder;
import com.hanbang.maige.code.base.baseadapter.recyclerview.CommonAdapter;
import com.hanbang.maige.code.base.baseadapter.recyclerview.click.OnItemClickListener;
import com.hanbang.maige.code.base.view.activity.BaseActivity;
import com.hanbang.maige.mode.javabean.quanzi.PoiData;
import com.hanbang.maige.utils.MyLocationUtils;
import com.hanbang.maige.utils.other.StringUtils;
import com.hanbang.maige.utils.ui.divider.HorizontalDividerItemDecoration;
import com.hanbang.maige.widget.AnimCheckBox;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;


/**
 * 作者　　: 李坤
 * 创建时间:2016/10/31　12:11
 * 邮箱　　：496546144@qq.com
 * <p>
 * 功能介绍：发布朋友圈activity
 */

public class LocationSelectActivity extends BaseActivity
        implements BDLocationListener, OnGetGeoCoderResultListener {
    @BindView(R.id.recycleView)
    RecyclerView recycleView;
    GeoCoder geoCoder = GeoCoder.newInstance();
    private ArrayList<PoiData> listDatas = new ArrayList<>();
    MyLocationUtils locationUtils;
    private PoiData selectPoiData;
    private int selectPosition;

    public static void startUI(BaseActivity context, PoiData poiData) {
        Intent intent = new Intent(context, LocationSelectActivity.class);
        if (poiData != null) {
            intent.putExtra("data", poiData);
        }
        context.startActivityForResult(intent, context.REQUEST_CODE + 2);
    }

    @Override
    public int getContentView() {
        return R.layout.activity_location_select;
    }


    CommonAdapter adapter = new CommonAdapter<PoiData>(this, R.layout.item_quanzi_poi_select, listDatas
    ) {
        @Override
        public void convert(ViewHolder holder, PoiData o) {
            holder.setText(R.id.title, o.getTitle());
            holder.setText(R.id.detaile, o.getAddress());
            holder.setVisible(R.id.detaile, o.address != null);
            AnimCheckBox animCheckBox = holder.getView(R.id.animCheckBox);

            holder.setVisible(R.id.animCheckBox, holder.getmPosition() == selectPosition);
            animCheckBox.setChecked(holder.getmPosition() == selectPosition);

        }

    };

    @Override
    public void initView() {
        recycleView.setLayoutManager(new LinearLayoutManager(this));
        recycleView.setLayoutManager(new LinearLayoutManager(this));
        recycleView.addItemDecoration(new HorizontalDividerItemDecoration.Builder(this)
                .sizeResId(R.dimen.dp_1).colorResId(R.color.gray_ee)
                .build());
        recycleView.setAdapter(adapter);
        locationUtils = new MyLocationUtils(this);

        geoCoder.setOnGetGeoCodeResultListener(this);
        adapter.setOnItemClickListener(new OnItemClickListener<PoiData>() {
            @Override
            public void onItemClick(ViewGroup parent, View view, PoiData data, int position) {
                Intent intent = new Intent();
                if (position != 0) {
                    intent.putExtra("data", data);
                }
                setResult(RESULT_OK, intent);
                finish();
            }
        });
    }

    @Override
    public void parseIntent(Intent intent) {
        selectPoiData = intent.getParcelableExtra("data");
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        locationUtils.start();
        showDialog("定位中");

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        geoCoder.destroy();
        locationUtils.stop();
    }


    @Override
    public void onReceiveLocation(BDLocation bdLocation) {

        if (bdLocation == null) {
            dismissDialog();
            return;
        }
        LatLng latlng = new LatLng(bdLocation.getLatitude(),
                bdLocation.getLongitude());
        showDialog("获取周围信息");
        ReverseGeoCodeOption option = new ReverseGeoCodeOption();
        option.location(latlng);
        listDatas.clear();

        listDatas.add(PoiData.getOneData());
        selectPosition = 0;

        PoiData d = new PoiData();
        d.setTitle(bdLocation.getCity());
        d.setLocation(latlng);
        listDatas.add(d);
        setSelectPosition(d);
        boolean b = geoCoder.reverseGeoCode(option);
        if (!b) {
            dismissDialog();
        }
    }

    @Override
    public void onGetGeoCodeResult(GeoCodeResult geoCodeResult) {

    }

    @Override
    public void onGetReverseGeoCodeResult(ReverseGeoCodeResult reverseGeoCodeResult) {
        dismissDialog();
        List<PoiInfo> poiInfos = reverseGeoCodeResult.getPoiList();
        if (poiInfos != null) {
            for (PoiInfo p : poiInfos) {
                PoiData d = new PoiData();
                d.setTitle(p.name);
                d.setLocation(p.location);
                d.setAddress(p.address);
                listDatas.add(d);
                setSelectPosition(d);
            }
        }


        adapter.notifyDataSetChanged();
    }

    private void setSelectPosition(PoiData d) {
        if (selectPoiData != null) {
            if (StringUtils.isEquals(selectPoiData.getTitle(), d.getTitle())) {
                selectPosition = listDatas.indexOf(d);
            }
        }
    }
}
